package modules.enlist
{
	import core.Language;
	import core.StageProxy;
	import core.manager.CostManager;
	import core.model.PlayerModel;
	import core.model.events.PlayerModelEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import framework.core.TipManager;
	import framework.utils.TextUtil;
	import framework.view.components.Component;
	import framework.view.components.button.ButtonProxy;
	import framework.view.components.container.IRender;
	import framework.view.components.container.RepeaterRenderProxy;
	
	import mx.utils.StringUtil;
	
	import values.enlist.EnlistNumTemplateValueList;
	import values.enlist.EnlistTemplateValue;
	import values.events.ItemValueEvent;
	import values.item.IItemValue;
	import values.item.INumbericItemValue;
	import values.item.ItemTemplateKeyType;
	import values.item.ItemTemplateValueList;
	import values.item.ItemValue;
	import values.skill.SkillTemplateValue;
	import values.skill.SkillTemplateValueList;
	
	import view.component.asset.IconProxy;
	import view.component.dialog.AlertDialog;
	import view.component.dialog.ConfirmDialog;
	import view.component.tip.AvatarPropertyTip;

	
	public class EnlistAvatarRenderProxy extends RepeaterRenderProxy
	{
		public function EnlistAvatarRenderProxy()
		{
			super();
		}
		
		[Inject]
		public var playerModel:PlayerModel;
		
		protected var vo:EnlistTemplateValue;
		
		override public function set data(value:*):void
		{
			vo = value;
			
			skin["avatar_name"].text = vo.avatarTemplate.name;
			skin["avatar_type"].text = vo.avatarTemplate.avatarType.name;
			skin["maxHPTF"].text = vo.avatarTemplate.maxHP.toString();
			skin["normalPowerTF"].text = vo.avatarTemplate.normalPower.toString();
			skin["skillPowerTF"].text = vo.avatarTemplate.skillPower.toString();
			skin["elementPowerTF"].text = vo.avatarTemplate.elementPower.toString();
			
			( skin["avatar_type"] as TextField ).mouseEnabled = false;
			icon.load(vo.avatarTemplate.getAssetByKey("icon").url);
			icon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			
			if(vo.avatarTemplate.defaultSkill)
			{
				normalSkillIcon.load(vo.avatarTemplate.defaultSkill.icon);
				normalSkillIcon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onNormalSkillIconMouseOver);
			}
			
			var ultimateSkill:SkillTemplateValue = SkillTemplateValueList.getByKey(vo.avatarTemplate.skill.ultimateSkill);
			if(ultimateSkill)
			{
				ultimateSkillIcon.load(ultimateSkill.icon);
				ultimateSkillIcon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onUltimateSkillIconMouseOver);
			}
			
			updataBtn();
			var costlist:Vector.<IItemValue> = vo.cost.costValueList;
			for each(var civ:IItemValue in costlist)
			{
				civ.addEventListener(ItemValueEvent.NUM_CHANGED , updataBtn);
			}
		}
		
		override public function parse(target:MovieClip):void
		{
			super.parse(target);
			skin.maxHPConstTF.text = Language.getText("@property_maxHP_name") + ":";
			skin.normalPowerConstTF.text = Language.getText("@property_normalPower_name") + ":";
			skin.skillPowerConstTF.text = Language.getText("@property_skillPower_name") + ":";
			skin.elementPowerConstTF.text = Language.getText("@property_normalPower_name") + ":";
			skin.reputationConstTF.text = Language.getText("@reputation_name") + ":";
		}
		
		private function updataBtn(evt:Event = null):void
		{
			var enableBool:Boolean = true;
			
			skin["reputationTF"].text = vo.reputation.toString();
			
			if(vo.reputation > playerModel.myPlayerAvatar.reputation)
			{
				enableBool = false;
				TextField(skin["reputationTF"]).textColor = 0xff0000;
			}
			else
			{
				TextField(skin["reputationTF"]).textColor = 0xffffff;
			}
			
			var costlist:Vector.<IItemValue> = vo.cost.costValueList;
			for each(var civ:IItemValue in costlist)
			{
				skin["costConstTF"].text = civ.template.name;
				skin["costTF"].text = ( civ as INumbericItemValue ).num;
				
				if(( civ as INumbericItemValue ).num > playerModel.getItemNum(civ.key))
				{
					enableBool = false;
					TextField(skin["costTF"]).textColor = 0xff0000;
				}
				else
				{
					TextField(skin["costTF"]).textColor = 0xffffff;
				}
			}
			
			enlistBtn.enabled = enableBool;
		}
		
		override public function dispose():void
		{
			
			icon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			
			if(vo.avatarTemplate.defaultSkill)
			{
				normalSkillIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onNormalSkillIconMouseOver);	
			}
			
			var ultimateSkill:SkillTemplateValue = SkillTemplateValueList.getByKey(vo.avatarTemplate.skill.ultimateSkill);
			if(ultimateSkill)
			{
				ultimateSkillIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onUltimateSkillIconMouseOver);
			}
			var costlist:Vector.<IItemValue> = vo.cost.costValueList;
			for each(var civ:IItemValue in costlist)
			{
				civ.removeEventListener(ItemValueEvent.NUM_CHANGED , updataBtn);
			}
			super.dispose();
		}
		
		override public function get data():*
		{
			return vo;
		}
		
		private function get icon():IconProxy
		{
			return proxys["icon"];
		}
		
		private function get normalSkillIcon():IconProxy
		{
			return proxys["normalSkillIcon"];
		}
		
		private function get ultimateSkillIcon():IconProxy
		{
			return proxys["ultimateSkillIcon"];
		}
		
		private function get enlistBtn():ButtonProxy
		{
			return proxys["enlistBtn"];
		}
		
		override protected function get proxyMapping():Array
		{
			return [ 
					{ skin:"icon", classRef: IconProxy, id:"icon" },
					{ skin:"normalSkillIcon", classRef: IconProxy, id:"normalSkillIcon" },
					{ skin:"ultimateSkillIcon", classRef: IconProxy, id:"ultimateSkillIcon" },
					{ skin:"enlistBtn", classRef: ButtonProxy, id:"enlistBtn", text:Language.getText("@enlist_btn_desc"), onClick: onClickHandler }
				];
		}
		
		private function onIconMouseOver(evt:Event):void
		{
			TipManager.showTip(icon.skinContent , vo.avatarTemplate , AvatarPropertyTip);
		}
		
		private function onUltimateSkillIconMouseOver(evt:Event):void
		{
			TipManager.showTip(ultimateSkillIcon.skinContent , vo.avatarTemplate.defaultSkill.desc);
		}
		
		private function onNormalSkillIconMouseOver(evt:Event):void
		{
			TipManager.showTip(normalSkillIcon.skinContent , (SkillTemplateValueList.getByKey(vo.avatarTemplate.skill.ultimateSkill).desc) );
		}
		
		
		
		private function onClickHandler():void
		{
			if(playerModel.myForce.allHeros.length - 1 < EnlistNumTemplateValueList.getCurrentNum(playerModel.myPlayerAvatar.level))
			{
				if(vo.cost.cashCost > 0)
				{
					ConfirmDialog.show(StringUtil.substitute(Language.getText("@confrim_enlist_hero") , 
					vo.cost.cashCost.toString() + ItemTemplateValueList.getByKey(ItemTemplateKeyType.CASH).name ,
					vo.avatarTemplate.name)  , onHireConfirm );
				}
				else
				{
					onHireConfirm();
				}
			}
			else
			{
				var levelObj:Object = EnlistNumTemplateValueList.getNextLevel(playerModel.myPlayerAvatar.level);
				ConfirmDialog.showAlert(StringUtil.substitute(Language.getText("@alert_no_space") , 
					                                        levelObj.level , levelObj.num));
			}	
		}
		
		private function onHireConfirm():void
		{
            StageProxy.lock();
			playerModel.hireHero(vo.avatarKey, ItemValue.convertNumbericItemsToServerObject(vo.cost.costValueList));
		}
	}
	
}